package com.agrm.rabc.controller;


import com.agrm.model.common.dtos.ResponseResult;
import com.agrm.model.rabc.dto.Menu;
import com.agrm.model.rabc.dto.Perm;
import com.agrm.model.rabc.dto.Resource;
import com.agrm.model.rabc.dto.Role;
import com.agrm.rabc.service.RbacService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping(value = "/api/v1/rbac", produces = MediaType.APPLICATION_JSON_VALUE)
public class RbacApi {
    private RbacService rbacService;

    @Autowired
    public void setRbacService(RbacService rbacService) {
        this.rbacService = rbacService;
    }


    @GetMapping({"/roles/{userId}/{valid}", "/roles/{userId}"})
    public ResponseResult findRolesByUserId(@PathVariable int userId,
                                            @PathVariable(required = false) Boolean valid) {
        List<Role> roles;
        if (Boolean.TRUE.equals(valid)) {//所有可用角色
            roles = rbacService.findValidRolesByUserId(userId);
        } else {//所有角色，包括不可用
            roles = rbacService.findAllRolesByUserId(userId);
        }
        List<String> roleNames = roles.stream().map(Role::getName).toList();
        return ResponseResult.okResult(roleNames);
    }

    //查询指定用户的所有权限
    @GetMapping({"/perms/{userId}/{valid}", "/perms/{userId}"})
    public ResponseResult findPermsByUserId(@PathVariable int userId,
                                                           @PathVariable(required = false) Boolean valid) {
        List<Perm> perms;
        if (Boolean.TRUE.equals(valid)) {//所有可用权限
            perms = rbacService.findValidPermsByUserId(userId);
        } else {//所有权限，包括不可用
            perms = rbacService.findAllPermsByUserId(userId);
        }
        //权限名称
        List<String> permNames = perms.stream().map(Perm::getName).toList();
        return ResponseResult.okResult(permNames);
    }

    //查询资源-权限的映射关系
    @GetMapping("/resource_perm_mappings")
    public ResponseResult findResourcePermMappings() {
        Map<Resource, List<Perm>> resourcePermMappings = rbacService.findAllResourcePermMappings();
        Map<String, List<String>> rtn = new HashMap<>();

        for (Map.Entry<Resource, List<Perm>> entry : resourcePermMappings.entrySet()) {
            Resource r = entry.getKey();
            List<Perm> perms = entry.getValue();
            List<String> strPerms = perms.stream().map(Perm::getName).toList();
            rtn.put(r.getMethod() + ":" + r.getValue(), strPerms);
        }

        return ResponseResult.okResult(rtn);
    }

    //查询指定用户所有菜单
    @GetMapping("/menus/{userId}")
    public ResponseResult findMenusByUserId(@PathVariable(value = "userId") int userId) {
        List<Menu> menus = rbacService.findAllMenusByUserId(userId);
        return ResponseResult.okResult(menus);
    }
}
